package com.jilian.mydemo.modul.main.ui;

import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.base.BaseEventMsg;
import com.jilian.mydemo.base.BaseFragment;
import com.jilian.mydemo.common.dto.CollectRefreshDto;
import com.jilian.mydemo.common.dto.MonthOrderMsgDto;
import com.jilian.mydemo.common.dto.MyInfoDto;
import com.jilian.mydemo.event.RxBus;
import com.jilian.mydemo.factory.Factoty;
import com.jilian.mydemo.modul.my.ui.HydraulicPerformanceActivity;
import com.jilian.mydemo.modul.my.ui.HydraulicRatingActivity;
import com.jilian.mydemo.modul.my.ui.MyCollectionActivity;
import com.jilian.mydemo.modul.my.ui.MyQrCodeActivity;
import com.jilian.mydemo.modul.my.ui.MyWalletActivity;
import com.jilian.mydemo.modul.my.ui.SettingActivity;
import com.jilian.mydemo.modul.user.activity.MyInfoActivity;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.UrlUtils;
import com.jilian.mydemo.views.CircularImageView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * 我的页面
 */
public class FourFragment extends BaseFragment {
    private ImageView ivSetting;
    private CircularImageView ivHead;
    private TextView tvName;
    private TextView tvArea;
    private TextView tvAccount;
    private TextView tvCount;
    private RelativeLayout rlOne;
    private ImageView ivOne;
    private RelativeLayout rlTwo;
    private ImageView ivTwo;
    private RelativeLayout rlThree;
    private ImageView ivThree;
    private RelativeLayout rlFour;
    private ImageView ivFour;
    private RelativeLayout rlFive;
    private ImageView ivFive;
    private RelativeLayout rlSix;
    private ImageView ivSix;
    private UserViewModel userViewModel;
    private LinearLayout llMonthOrder;
    private SmartRefreshLayout srHasData;


    @Override
    protected void loadData() {

    }

    @Override
    protected void createViewModel() {
        userViewModel = Factoty.getViewModel(UserViewModel.class, FourFragment.this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_four;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        srHasData = (SmartRefreshLayout) view.findViewById(R.id.sr_has_data);
        ivSetting = (ImageView) view.findViewById(R.id.iv_setting);
        ivHead = (CircularImageView) view.findViewById(R.id.iv_head);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvArea = (TextView) view.findViewById(R.id.tv_area);
        tvAccount = (TextView) view.findViewById(R.id.tv_account);
        tvCount = (TextView) view.findViewById(R.id.tv_count);
        rlOne = (RelativeLayout) view.findViewById(R.id.rl_one);
        ivOne = (ImageView) view.findViewById(R.id.iv_one);
        rlTwo = (RelativeLayout) view.findViewById(R.id.rl_two);
        ivTwo = (ImageView) view.findViewById(R.id.iv_two);
        rlThree = (RelativeLayout) view.findViewById(R.id.rl_three);
        ivThree = (ImageView) view.findViewById(R.id.iv_three);
        rlFour = (RelativeLayout) view.findViewById(R.id.rl_four);
        ivFour = (ImageView) view.findViewById(R.id.iv_four);
        rlFive = (RelativeLayout) view.findViewById(R.id.rl_five);
        ivFive = (ImageView) view.findViewById(R.id.iv_five);
        rlSix = (RelativeLayout) view.findViewById(R.id.rl_six);
        ivSix = (ImageView) view.findViewById(R.id.iv_six);
        llMonthOrder = (LinearLayout) view.findViewById(R.id.ll_month_order);
        srHasData.setEnableLoadMore(false);

    }

    @Override
    protected void initData() {
        getMyInfo();
    }

    /**
     * 初始化个人数据
     */
    private void initPersonData(MyInfoDto dto) {
        tvName.setText(dto.getUserName());
        Glide
                .with(getActivity())
                .load(UrlUtils.getUrl(dto.getUserImg()))
                .error(R.drawable.iv_default_head)
                .into(ivHead);
        if (EmptyUtils.isNotEmpty(dto.getServiceAreas())) {
            tvArea.setText(dto.getServiceAreas().get(0).getProvince() + dto.getServiceAreas().get(0).getCity() + dto.getServiceAreas().get(0).getDistrict());
        }
        tvAccount.setText("￥" + dto.getMonthCommission());
        tvCount.setText(dto.getOrderTotal());

    }

    /**
     * 获取个人信息数据
     */
    private void getMyInfo() {
        userViewModel.info();
        userViewModel.getInfoliveData().observe(this, new Observer<BaseDto<MyInfoDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<MyInfoDto> myInfoDtoBaseDto) {
                srHasData.finishRefresh();
                if (myInfoDtoBaseDto.isSuccess()) {
                    MyInfoDto dto = myInfoDtoBaseDto.getData();
                    initPersonData(dto);
                }
            }
        });
    }

    @Override
    protected void initListener() {
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getMyInfo();
            }
        });
        RxBus.getInstance().toObservable().map(new Function<Object, BaseEventMsg>() {
            @Override
            public BaseEventMsg apply(Object o) throws Exception {
                return (BaseEventMsg) o;
            }
        }).subscribe(new Consumer<BaseEventMsg>() {
            @Override
            public void accept(BaseEventMsg eventMsg) throws Exception {
                if (eventMsg != null && eventMsg.getMyInfoRefreshDto() != null) {
                    if (eventMsg.getMyInfoRefreshDto().getCode() == 100) {
                        getMyInfo();
                    }
                }
            }
        });
        llMonthOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MonthOrderMsgDto monthOrderMsgDto = new MonthOrderMsgDto();
                monthOrderMsgDto.setCode(100);
                BaseEventMsg msg = new BaseEventMsg();
                msg.setMonthOrderMsgDto(monthOrderMsgDto);
                RxBus.getInstance().post(msg);
                ((MainActivity) getActivity()).viewPager.setCurrentItem(1);
            }
        });
        ivHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyInfoActivity.class));
            }
        });
        rlOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), HydraulicRatingActivity.class));
            }
        });
        rlTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), HydraulicPerformanceActivity.class));
            }
        });
        rlThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyWalletActivity.class));
            }
        });
        rlFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyQrCodeActivity.class));
            }
        });
        rlFive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyCollectionActivity.class));
            }
        });
        rlSix.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });
        ivSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });

    }
}
